package com.cccyy.encry;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class AES256Util {

  private static final String KEY = "U2FsdGVkX1+xC4WJafqUh+lHRawOaoPrXnocnvKcDHo="; // 从服务器要的密钥

  public static final String CIPHER_ALGORITHM = "AES";

  public static byte[] decrypt(byte[] data) throws Exception {

    Key k = toKey(KEY.getBytes());

    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, k);

    return cipher.doFinal(data);
  }

  private static Key toKey(byte[] key) throws Exception {

    SecretKey secretKey = new SecretKeySpec(key, "AES");

    return secretKey;
  }
//46EBA22EF5204DD5B110A1F730513965
  public static void main(String[] args) throws Exception {
    String pwdEn =
        "U2FsdGVkX1+xC4WJafqUh+lHRawOaoPrXnocnvKcDHo=";
    byte[] decrypt = AES256Util.decrypt(pwdEn.getBytes(StandardCharsets.UTF_8));
    String string = decrypt.toString();
    System.out.println("解密后数据为:" + string);
  }
}
